package cn.org.timespace.easyframe.demo;

/**
 * 会员发展数报表实现
 */

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.org.timespace.easyframe.util.datatime.DateMonthUtil;
import cn.org.timespace.easyframe.util.report.model.Column;
import cn.org.timespace.easyframe.util.report.model.DataTable;
import cn.org.timespace.easyframe.util.report.model.Foot;
import cn.org.timespace.easyframe.util.report.model.FootSumColumn;
import cn.org.timespace.easyframe.util.report.model.FootTextColumn;
import cn.org.timespace.easyframe.util.report.model.LabelProvider;
import cn.org.timespace.easyframe.util.report.model.Report;

public class MmsMemberDevReportServiceImpl implements DemoReportService {
	private static final Log log = LogFactory.getLog(MmsMemberDevReportServiceImpl.class);

	private static MmsMemberDevReportServiceImpl mmsMemberDevReportServiceImpl;
	
	private MmsMemberDevReportServiceImpl(){
	}
	
	public static DemoReportService getInstance(){
		if (mmsMemberDevReportServiceImpl == null) 
			mmsMemberDevReportServiceImpl = new MmsMemberDevReportServiceImpl();
		return mmsMemberDevReportServiceImpl;
	}
	
	private static final Object COLUMN_KEY_REPORT_NAME = "report_name";

	public Report getReport() throws Exception {

		Report rpt = new Report();
		
		String months = "201101,201102,201103";
		List<String> monthList = DateMonthUtil.stringToList(months);

		// 构建报表表头
		buildReportHeader(rpt, monthList);
		
		//填充报表数据
		buildReportData(rpt,months,"10");
		
		// 合计
		buildClientTotalFoot(rpt,monthList);

		return rpt;
	}

	// 构建报表表头
	private void buildReportHeader(Report rpt, List<String> monthList) {
		// 第一行是报表名称，
		Column reportTitleCol = new Column(rpt);
		reportTitleCol.setTitle("会员发展数统计");
		reportTitleCol.setKey(COLUMN_KEY_REPORT_NAME);

		// 第二行
		Column orgCol = new Column(reportTitleCol);
		orgCol.setKey("org");
		orgCol.setTitle("单位");

		// 设置合计的计算规则
		Map<String, String> relative2Map = new HashMap<String, String>();
		String exp2 = "";

		// 获得统计的月份数
		for (int i = 0; i < monthList.size(); i++) {
			String month = monthList.get(i);

			Column monthCol = new Column(reportTitleCol);
			monthCol.setKey("month" + month);
			monthCol.setTitle(month.substring(0, 4) + "年"
					+ month.substring(4, 6) + "月");

			Column genCol = new Column(monthCol);
			genCol.setKey("gen" + month);
			genCol.setTitle("普通");
			genCol.setLabelProvider(LabelProvider
					.getNumberLabelProvider("#########"));

			Column saveCol = new Column(monthCol);
			saveCol.setKey("save" + month);
			saveCol.setTitle("储值");
			saveCol.setLabelProvider(LabelProvider
					.getNumberLabelProvider("#########"));

			Column fixCol = new Column(monthCol);
			fixCol.setKey("fix" + month);
			fixCol.setTitle("定额");
			fixCol.setLabelProvider(LabelProvider
					.getNumberLabelProvider("#########"));

			Column subtotalCol = new Column(monthCol);
			subtotalCol.setKey("subtotalCol" + month);
			subtotalCol.setTitle("小计");
			subtotalCol.setLabelProvider(LabelProvider
					.getNumberLabelProvider("#########"));

			// 设置小计的计算规则
			Map<String, String> relative1Map = new HashMap<String, String>();
			relative1Map.put("a", genCol.getKey().toString());
			relative1Map.put("b", saveCol.getKey().toString());
			relative1Map.put("c", fixCol.getKey().toString());
			String exp1 = "a+b+c";
			rpt.getDataTable().setComputeColumn(subtotalCol.getKey(),
					relative1Map, exp1);

			// 设置合计的计算规则
			relative2Map.put(subtotalCol.getKey().toString(), subtotalCol
					.getKey().toString());
			if (exp2.equals(""))
				exp2 = subtotalCol.getKey().toString();
			else
				exp2 = exp2 + "+" + subtotalCol.getKey().toString();

		}

		Column totalCol = new Column(reportTitleCol);
		totalCol.setKey("total");
		totalCol.setTitle("合计");
		totalCol.setLabelProvider(LabelProvider
				.getNumberLabelProvider("#########"));

		rpt.getDataTable().setComputeColumn(totalCol.getKey(), relative2Map,exp2);
	}
	
	//合计
	private void buildClientTotalFoot(Report rpt,List<String> monthList){
		Foot foot = new Foot(rpt);
		FootTextColumn hTotalCol = new FootTextColumn(foot, rpt.getColumn("org"));
		hTotalCol.setText("合计：");
		
		//获得统计的月份数
		for(int i=0;i<monthList.size();i++){
			String month = monthList.get(i);
			new FootSumColumn(foot, rpt.getColumn("gen"+month),LabelProvider.getNumberLabelProvider("#########"));
			new FootSumColumn(foot, rpt.getColumn("save"+month),LabelProvider.getNumberLabelProvider("#########"));
			new FootSumColumn(foot, rpt.getColumn("fix"+month),LabelProvider.getNumberLabelProvider("#########"));
			new FootSumColumn(foot, rpt.getColumn("subtotalCol"+month),LabelProvider.getNumberLabelProvider("#########"));
		}
		
		new FootSumColumn(foot, rpt.getColumn("total"),LabelProvider.getNumberLabelProvider("#########"));
	}
	
	//填充报表数据
	private void buildReportData(Report rpt,String months,String orgID) throws Exception{
		DataTable dt = rpt.getDataTable();
		dt.setData("rowkeyOrg1", "org", "广州泗合信息技术有限公司");
		dt.setData("rowkeyOrg2", "org", "广州泗合");
		
//		//获取机构
//		List<String> orgList = mmsReportDao.queryAllOrg(orgID);
//		
//		for(int i=0;i<orgList.size();i++){
//			String[] org = orgList.get(i).split(":");
//			dt.setData("rowkeyOrg"+org[0], "org", org[1]);
//		}
//		
//		
//		//设置列    数据根据查询条件从数据库获取
//		//colList.add("01:201010:gen:10");
//		List<MmsMember> colList = mmsReportDao.queryAddedMemberNum(months,orgID);		
//		MmsMember mmsMember=null;
//		for(int i=0;i<colList.size();i++){
//			mmsMember = colList.get(i);
//			String type="";
//			if(mmsMember.getDicMemberType().equals("0")){
//				type="gen";
//			}else if(mmsMember.getDicMemberType().equals("1")){
//				type="save";
//			}else if(mmsMember.getDicMemberType().equals("2")){
//				type="fix";
//			}
//			dt.setData("rowkeyOrg"+mmsMember.getMobileNo(),type+mmsMember.getBusiDatetime(),mmsMember.getEmail());
//		}
	}

}
